using System.IO;
using System.Text;
using Toast.Compression.Providers;
using Toast.Core;
using Toast.Core.Interfaces;

namespace Toast.Compression
{
    public class JsMinCompression : IJavaScriptCompressionStrategy
    {
        #region IJavaScriptCompressionStrategy Members

        public string Compress(string script)
        {
            var minifier = new JavaScriptMinifier();
            var input = new MemoryStream(Encoding.UTF8.GetBytes(script));
            var output = new MemoryStream();

            minifier.Minify(input, output);

            output.Seek(0, SeekOrigin.Begin);

            using (var reader = new StreamReader(output))
                return reader.ReadToEnd();
        }

        public CompressionLevel CompressionLevel { get; set; }

        #endregion
    }
}